<?php

namespace App\Repositories;

use App\Models\DeveloperLink;
use Cache;
use TestingConfig;
use TestingDetailService;
use TestingDeveloperService;
use App\Models\LogHelper;
class DeveloperLinkRepository extends Repository
{
    static $tag = 'testing.developer.link';
    


    public function getById($id)
    {
        $cacheKey = getCacheKey('testing.developer.link.info') . $id;
        $value = $this->remember($cacheKey, function () use ($id) {
            return DeveloperLink::where('id', $id)->first();
        });
        return $value;
    }


     public function store($data)
     {
        $this->clearCache($this->tag());
        $res = DeveloperLink::firstOrCreate($data);
        return $res;
     }


    public function getPositionOption(){
        $options = [];
        $optionStr = TestingConfig::getValue('testing.developer.position.option');
        if($optionStr){
            $arr = explode(',',$optionStr);
            foreach ($arr as $val){
                $optionArr = explode(':',$val);
                $id = intval($optionArr[0]);
//                $obj['id'] = $id?$id:1;
//                $obj['text'] = $optionArr[1]?$optionArr[1]:'';
                $options[$id] = $optionArr[1]?$optionArr[1]:'';
//                array_push($options,$obj);
            }
        }
        return $options;
    }


    /**
     * 根据union_code获取链接
     */
    public function getLinksByUnionCode($unionCode){
        $cacheKey = getCacheKey('testing.developer_link.list') . $unionCode;
        $value = $this->remember($cacheKey, function () use ($unionCode) {
            return DeveloperLink::where('union_code', $unionCode)->get();
        });
        return $value;
    }

    public function getClientLink($unionCode){
        $links = $this->getLinksByUnionCode($unionCode);
        $link = '';
        if(in_weixin()||in_windows_weixin()){
            $link = $this->getClientLinkByType($unionCode,1,$links);
        }else{
            $link = $this->getClientLinkByType($unionCode,3,$links);
        }
        return $link;
    }

    public function getClientLinkByTid($tid,$thirdpartyName = ''){
        if($thirdpartyName){
            //union_code直接从开发者表中进行查找
            $developer = TestingDeveloperService::getByAbbreviation($thirdpartyName);
            $unicode = $developer['union_code'];
        }else{
            $testingInfo = TestingDetailService::getById($tid);
            $unicode = $testingInfo['dev_union_code'];
        }

        return $this->getClientLink($unicode);
    }


    public function getClientLinkByType($unionCode,$clientType = 1,$links){
        $links = $links ? $links : $this->getLinksByUnionCode($unionCode);
        $value = [];
        foreach ($links as $obj){
            if($obj['position'] == $clientType){
                $value['name'] = $obj['name'];
                $value['url'] = $obj['url'];
                break;
            }
        }
        $result = count($value)>0 ? $value : "";
        return $result;
    }




    public function cacheTime()
    {
        return config('testing.common_cache_time');
    }


    public function model()
    {
        return app(DeveloperLink::class);
    }

    public function tag()
    {
        return self::$tag;
    }
}